import { ElMessage } from 'element-plus'
import { deleteUser } from '@/api/system/admin'
const single = ref(true)
const multiple = ref(true)
/*table 列表*/
const ids = ref([])
const totalNum = ref(0)
const loading = ref(false)
const userList = ref([])
const showSearch = ref(true)
const refAddEditModal = ref('')

const refDetailedModal = ref('')

const refElTable = ref(null)

const tableHeadColumns = ref([
  { prop: 'username', label: ('user.username'), minWidth: 100, isTemplate: false, showColumn: true },
  { prop: 'name', label: ('user.name'), minWidth: 100, isTemplate: false, showColumn: true },
  { prop: 'id', label: ('user.userid'), minWidth: 100, isTemplate: false, showColumn: true },
  { prop: 'email', label: ('user.email'), minWidth: 100, isTemplate: false, showColumn: true },
  { prop: 'phone', label: ('user.phone'), minWidth: 120, isTemplate: false, showColumn: true },
  { prop: 'role', label: ('user.role'), minWidth: 80, isTemplate: true, align: 'center', showColumn: true },
  { prop: 'create_at', label: ('user.createtime'), minWidth: 140, isTemplate: false, align: 'center', showColumn: true },
  { prop: 'update_at', label: ('user.updatetime'), minWidth: 140, isTemplate: false, align: 'center', showColumn: true }
])
export const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.userId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
export const colChange = (heardColsArr) => {
  tableHeadColumns.value = heardColsArr
}

export const handleAdd = () => {
  refAddEditModal.value.showModal({})
}

export const removeEmptyKey = (data) => {
  return Object.keys(data)
    .filter((key) => data[key] !== null && data[key] !== undefined && data[key] !== '')
    .reduce((acc, key) => ({ ...acc, [key]: data[key] }), {})
}

watch(
  () => tableHeadColumns,
  () => {
    if (refElTable) {
      nextTick(refElTable.value.doLayout)
    }
  },
  { deep: true }
)
export const currentHook = (queryParams, getList) => {

  const handleDelete = (row) => {
    const userIds = row.id || row.userId
    elConfirm(`是否确认删除用户编号为"${userIds}"的数据项`)
      .then(() => {
        return deleteUser(userIds)
      })
      .then(() => {
        ElMessage({ message: '删除成功', type: 'success' })
        getList()
      })
  }
  return {
    refAddEditModal,
    refDetailedModal,
    handleDelete,
    refElTable,
    multiple,
    ids,
    single,
    totalNum,
    loading,
    userList,
    showSearch,
    tableHeadColumns
  }
}
